/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_score_write.js                            
*  Description  : 스코어 등록
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-03  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 나의 예약 정보
	function clsMyReserveList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this._ERRMSG = {
			"_NOVALUE": "예약선택이나 직접입력을 선택해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsMyReserveList.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = [{
				"예약코드": ""
				, "라운드일자": ""
				, "라운드시간": ""
				, "골프장코스": ""
				, "홀": ""
				, "파": []
			}].concat(thisData);
			this.display();
		}
		, setPar: function(reserveKey, thisData){
			for(var i=0;i<this.listData.length;i++){
				var obj = this.listData[i];
				
				if(obj["예약코드"]+"" == reserveKey+"") break;
			}
			
			if(i < this.listData.length){
				this.listData[i]["파"] = thisData;
			}
		}
		, setIdx: function(n){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("select option").eq(n).attr("SELECTED", "SELECTED");
			$layer.find("select").trigger("change");
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			$layer.find("select").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("select").append("<option />");
			}
			$layer.find("select option").each(function(n){
				var obj = thisClass.listData[n];
				
				if(obj["예약코드"]){
					$(this).text(obj["라운드일자"] + " " + obj["라운드시간"] + obj["골프장코스"] + obj["홀"]);
				}else{
					$(this).text(" ---- 예약 선택 -- ");
				}
				
			});
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var ck = $layer.find("input:checkbox").is(":checked");
			
			if(ck){
				return -1;
			}else{
				var $select = $layer.find("select");
				
				return $select.find("option").index($select.find("option:selected"));
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, getRoundDate: function(){
			var $layer = this._HTMLDom["$layer"];
			var $select = $layer.find("select");
			var n = $select.find("option").index($select.find("option:selected"));
			
			return this.listData[n]["라운드일자"];
		}
		, getGolfCourse: function(){
			var $layer = this._HTMLDom["$layer"];
			var $select = $layer.find("select");
			var n = $select.find("option").index($select.find("option:selected"));
			
			return this.listData[n]["골프장코스"];
		}
		, getHole: function(i){
			var $layer = this._HTMLDom["$layer"];
			var $select = $layer.find("select");
			var n = $select.find("option").index($select.find("option:selected"));
			
			return this.listData[n]["홀" + i];
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			this.errMsg = null;
			
			if(this.getValue() == 0){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].find("select").trigger("focus");
		}
		, getReserveKey: function(){
			var n = this.getValue();
			
			if(n < 0){
				return "";
			}else{
				return this.listData[n]["예약코드"];
			}
		}
		, setReserveKey: function(key){
			var $layer = this.$getLayer();
			
			if((key+"").substrEx(0, 1) == "D"){
				$layer.find("input:checkbox").attr("CHECKED", "CHECKED");
			}else{
				for(var i=0;i<this.listData.length;i++){
					if(this.listData["예약코드"]+"" == key+"") break;
				}
				
				if(i<this.listData.length){
					$layer.find("select option").eq(i).attr("SELECTED", "SELECTED");
					$layer.find("select").trigger("change");
				}
			}
			
			this.disable();
		}
		, disable: function(){
			var $layer = this.$getLayer();
			
			$layer.find("select").prop("disabled", true);
			$layer.find("input:checkbox").prop("disabled", true);
		}
		, getGolfKey: function(){
			var n = this.getValue();
			
			if(n < 0){
				return "";
			}else{
				return this.listData[n]["골프장코드"];
			}
		}
		, getCourseKey1: function(){
			var n = this.getValue();
			
			if(n < 0){
				return "";
			}else{
				return this.listData[n]["코스코드1"];
			}
		}
		, getCourseKey2: function(){
			var n = this.getValue();
			
			if(n < 0){
				return "";
			}else{
				return this.listData[n]["코스코드2"];
			}
		}
		, getParValues: function(){
			var n = this.getValue();
			
			if(n < 0){
				return this.listData[0]["파"];
			}else{
				return this.listData[n]["파"];
			}
		}
	}
	
	// 라운드일자
	function clsRoundDate()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "라운드일자를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsRoundDate.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text").prop("readonly", true).addClass("inp").css("margin-right", "10px").datepicker({
				showOn: "both"
				, buttonImage: _ROOT + "/images/btn/icon_calendar.gif"
				, buttonImageOnly: true
				, disabled: true
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setEditable: function(val){
			var s = (val ? "enable": "disable");
			
			this.$getLayer().find("input:text").datepicker(s);
		}
		, getValue: function(){
			var $layer =  this.$getLayer();
			return $layer.find("input:text").val();
		}
		, setValue: function(val){
			var $layer = this.$getLayer();
			$layer.find("input:text").val(val);
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer =  this.$getLayer();
			var b = $layer.find("input:text");
			
			this.errMsg = null;
			
			if(!b.val()){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			var $layer = this._HTMLDom["$layer"];
			var b = $layer.find("input:text");
			
			b.trigger("focus");
		}
	}
	
	// 골프장코스
	function clsGolfCourse()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "골프장코스를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsGolfCourse.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(2)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text").addClass("inp");
		}
		, display:clsRoundDate.prototype.display
		, $getLayer: clsRoundDate.prototype.$getLayer
		, getValue: clsRoundDate.prototype.getValue
		, setValue: clsRoundDate.prototype.setValue
		, getError: clsRoundDate.prototype.getError
		, valid: clsRoundDate.prototype.valid
		, focus: clsRoundDate.prototype.focus
		, setEditable: function(val){
			this.$getLayer().find("input:text").prop("readonly", !val);
		}
	}
	
	// 홀
	function clsHole()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "홀을 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsHole.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(3)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text").addClass("inp");
		}
		, $getLayer: clsRoundDate.prototype.$getLayer
		, display:clsRoundDate.prototype.display
		, editable:clsGolfCourse.prototype.editable
		, getValue: clsRoundDate.prototype.getValue
		, setValue: clsRoundDate.prototype.setValue
		, getError: clsRoundDate.prototype.getError
		, valid: clsRoundDate.prototype.valid
		, focus: clsRoundDate.prototype.focus
		, setEditable: clsGolfCourse.prototype.setEditable
	}
	
	// 사진첨부
	function clsUploadFile()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsUploadFile.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(4)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text:eq(0)").prop("readonly", true);
			
			this.setInfo({
				"파일코드": ""
				, "파일명": ""
				, "원본파일명": ""
			});
			
			var $i = $layer.find("img[alt='사진첨부']");
			var p = $i.position();
			_gf.setUploadFileImage($i, null, function(ret){
				document.objControl.finishUploadFile(ret);
			}, {
				"left": p["left"]
				, "top": p["top"] - 22
			});
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("input:text:eq(0)").val(this.infoData["원본파일명"]);
		}
		, getFileKey: function(){
			return this.infoData["파일코드"];
		}
		, getFilePath: function(){
			return this.infoData["파일명"];
		}
		, getOrigFileName: function(){
			return this.infoData["원본파일명"];
		}
	}
	
	// 내용
	function clsNote()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsNote.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round tbody tr:eq(4)");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, getValue: function(){
			return this.$getLayer().find("textarea").val();
		}
		, setValue: function(val){
			this.$getLayer().find("textarea").val(val);
		}
	}
	
	// 스코어카드
	function clsScoreCard()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsScoreCard.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list01 table");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:eq(1) td:eq(0)").clone(true)
			}
			
			this.setTable(0);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setTable: function(val){
//			var $layer = this.$getLayer();
//			var thisClass = this;
//			var $element = this._HTMLDom["$element"];
//			var m = val/2;
//			
//			$layer.find("thead tr th:gt(0)").filter(function(){
//				return !$(this).attr("class");
//			}).remove();
//			$layer.find("tbody td").filter(function(){
//				return !$(this).attr("class");
//			}).remove();
//			
//			for(var i=0;i<val;i++){
//				if(i < m){
//					$layer.find("thead tr th:eq(0)").after("<th />");
//					$layer.find("tbody tr th:nth-child(1)").after("<td />");
//				}else{
//					$layer.find("thead tr th.tb_c:eq(0)").after("<th />");
//					$layer.find("tbody tr").each(function(){
//						$(this).children("td.tb_c:eq(0)").after("<td />");
//					});
//				}
//			}
//			
//			$layer.find("thead tr th:gt(0)").filter(function(){
//				return !$(this).attr("class");
//			}).each(function(n){
//				$(this).html(n+1);
//			});
//			
//			$layer.find("tbody tr:eq(0) td").filter(function(){
//				return !$(this).attr("class");
//			}).each(function(n){
//				$(this).html(0);
//			});
//			
//			$layer.find("tbody tr:eq(1) td").filter(function(){
//				return !$(this).attr("class");
//			}).each(function(n){
//				$(this).html($element.clone(true).children())
//			});
//			
//			$layer.find("tbody tr:eq(2) td").filter(function(){
//				return !$(this).attr("class");
//			}).each(function(n){
//				$(this).html(0);
//			});
		}
	}
	
	// 홀별파
	function clsScoreCardPar()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsScoreCardPar.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list01 tbody>tr:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
			this.calculate();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			this.$getDataCell().each(function(n){
				var par = _util.nvl(thisClass.listData[n], 0);
				
				$(this).html(par);
			});
		}
		, editable: function(dValues){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			if(!dValues) dValues = [];
			this.$getDataCell().each(function(n){
				var par = _util.nvl(dValues[n], 0);
			
				$(this).html("<input type='text' style='width:25px;' class='inp' maxlength='2' value='" + par + "'>");
			});
			this.calculate();
		}
		, $getDataCell: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("td").filter(function(){
				return !$(this).attr("class");
			});
		}
		, calculate: function(){
			var $layer = this._HTMLDom["$layer"];
			var len = $layer.find("input:text").length;
			
			var sTotal = {
				"소계0": 0
				, "소계1": 0
			}
			
			var m = 0;
			if(len > 0){
				m = $layer.find("input:text").length/2;
				$layer.find("input:text").each(function(n){
					if(!$(this).val()) $(this).val("0");
					
					if(n < m) sTotal["소계0"] += parseInt($(this).val());
					else sTotal["소계1"] += parseInt($(this).val());
				});
			}else{
				if(!this.listData) return;
				var m = this.listData.length/2;
				for(var i=0;i<this.listData.length;i++){
					if(i < m) sTotal["소계0"] += this.listData[i];
					else sTotal["소계1"] +=  this.listData[i];
				}
			}
			
			$layer.find("td.tb_c").each(function(n){
				$(this).html(sTotal["소계" + n]);
			});
			
			$layer.find("td.total").html(sTotal["소계0"] + sTotal["소계1"]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setValues: function(aVal){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("input:text").each(function(n){
				$(this).val(_util.nvl(aVal[n], 0));
			});
			this.calculate();
		}
		, getValues: function(){
			var $layer = this._HTMLDom["$layer"];
			var len = $layer.find("input:text").length;
			var aRet = [];
			
			if(len > 0){
				$layer.find("input:text").each(function(){
					aRet.push($(this).val());
				});
			}else{
				aRet = this.listData;
			}
			
			return aRet;
		}
		, getValue: function(n){
			var $layer = this._HTMLDom["$layer"];
			var len = $layer.find("input:text").length;
			var ret;
			
			if(len > 0){
				ret = $layer.find("input:text").eq(n).val();
			}else{
				ret = _util.nvl(this.listData[n], 0);
			}
			
			return ret;
		}
	}
	
	// 홀별 타
	function clsScoreCardTa()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsScoreCardTa.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list01 tbody>tr:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			$layer.find("input:text").attr("maxlength", "3");
			
			this.setValues([]);
		}
		, calculate: clsScoreCardPar.prototype.calculate
		, $getLayer: clsScoreCardPar.prototype.$getLayer
		, setValues: clsScoreCardPar.prototype.setValues
		, setValue: function(n, val){
			var $layer = this._HTMLDom["$layer"];
			
			val = parseInt(val)-0;
			if(isNaN(val)) val = 0;
			
			var cVal = $layer.find("input:text").eq(n).val()-0;
			
			$layer.find("input:text").eq(n).val(cVal + val);
			
			this.calculate();
		}
		, getValues: clsScoreCardPar.prototype.getValues
		, getValue: clsScoreCardPar.prototype.getValue
	}
	
	// 스코어카드 스코어
	function clsScoreCardScore()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsScoreCardScore.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list01 tbody>tr:eq(2)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("th:first").html("스코어");
			
			this.setList({
				"파":[]
				, "타": []
			});
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			this.$getDataCell().each(function(n){
				var par = parseInt(thisClass.listData["파"][n]) || 0;
				var ta = parseInt(thisClass.listData["타"][n]) || 0;
				
				$(this).html(par+ta);
			});
			
			var sTotal = {
				"소계0": 0
				, "소계1": 0
			}
			
			var m = this.listData.length/2;
			
			for(var i=0;i<this.listData["파"].length;i++){
				var par = parseInt(this.listData["파"][i]) || 0;
				var ta = parseInt(this.listData["타"][i]) || 0;
				
				if(i < m) sTotal["소계0"] += par +ta;
				else sTotal["소계1"] += par +ta;
			}
			
			$layer.find("td.tb_c").each(function(n){
				$(this).html(sTotal["소계" + n]);
			});
			
			$layer.find("td.total").html(sTotal["소계0"] + sTotal["소계1"]);
		}
		, getValues: function(){
			var thisClass = this;
			var aRet = [];
			
			this.$getDataCell().each(function(n){
				var par = parseInt(thisClass.listData["파"][n]) || 0;
				var ta = parseInt(thisClass.listData["타"][n]) || 0;
				
				aRet.push(par+ta);
			});
			
			return aRet;
		}
		, $getDataCell: clsScoreCardPar.prototype.$getDataCell
	}
	
	// 스코어 카드요약
	function clsScoreCardStat()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsScoreCardStat.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list02");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setList({
				"파": []
				, "타": []
			});
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			var sum = {
				"4이상": 0
				, "트리플보기": 0
				, "더블보기": 0
				, "보기": 0
				, "파": 0
				, "버디": 0
				, "이글": 0
				, "홀인원": 0
			}
			
			var aMap = [
				"4이상"
				, "트리플보기"
				, "더블보기"
				, "보기"
				, "파"
				, "버디"
				, "이글"
				, "홀인원"
			];
			
			for(var i=0;i<this.listData["파"].length;i++){
				var p = parseInt(this.listData["파"][i]);
				var t = parseInt(this.listData["타"][i]);
				
				p = p || 0;
				t = t || 0;
				
				if(p < 3 || p > 5) continue;
				
				if(t >= 0){
					switch(t){
						case 0:
							sum["파"]++;
							break;
						case 1:
							sum["보기"]++;
							break;
						case 2:
							sum["더블보기"]++;
							break;
						case 3:
							sum["트리플보기"]++;
							break;
						default:
							sum["4이상"]++;
							break;
					}
				}else if(t == -1){
					sum["버디"]++;
				}else if((p + t) == 1){
					sum["홀인원"]++;
				}else if((p == 4 || p == 5) && t == -2){
					sum["이글"]++;
				}
			}
			
			$layer.find("tbody td").each(function(n){
				$(this).html(sum[aMap[n]]);
			});
		}
	}
	
	// 서버통신
	function clsComm()
	{
		// 나의 예약정보
		this.myReserveList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.myReserveList = [];
		}
		// 나의 예약 리스트
		, getMyReserveList: function(arg){
			var thisClass = this;
			
			this.myReserveList = [];
			//ajax
			var serviceName = "getUserReservList";
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, {
				
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var reserveTime = _util.nvl(obj["reservTime"]);
						
						thisClass.myReserveList.push({
							"예약코드": _util.nvl(obj["reservSeq"])
							, "라운드일자": _util.nvl(obj["reservDate"]).substr(0, 10)
							, "라운드시간": reserveTime.substr(0, 2) + ":" + reserveTime.substr(2, 2)
							, "골프장코드": _util.nvl(obj["golfclubId"])
							, "골프장코스": _util.nvl(obj["golfclubName"]) + "-" + _util.nvl(obj["courseInfoName"])
							, "코스코드1": _util.nvl(obj["courseInfoId1"])
							, "코스코드2": _util.nvl(obj["courseInfoId2"])
							, "홀1": _util.nvl(obj["holeCnt1"], 0)
							, "홀2": _util.nvl(obj["holeCnt2"], 0)
							, "홀3": _util.nvl(obj["holeCnt3"], 0)
							, "홀": _util.nvl(obj["holeCnt1"], 0) + _util.nvl(obj["holeCnt2"], 0) + _util.nvl(obj["holeCnt3"], 0)
						});
					}
				}
				
				document.objControl.finishMyReserveList(res);
			});
		}
		, getMyReserveData: function(){
			return this.myReserveList;
		}
		// 코스정보 상세
		, getParList: function(arg){
			var thisClass = this;
		
			//ajax
			var serviceName = "getUserReservScoreCard";
			var options = {
				"reservSeq": arg["예약코드"]
				, "golfclubId": arg["골프장코드"]
				, "courseInfoId1": arg["코스코드1"]
				, "courseInfoId2": arg["코스코드2"]
				, "courseInfoId3": ""
			};
			
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "예약코드": arg["예약코드"]
					, "파": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					for(var i=1;i<19;i++){
						res["파"].push(_util.nvl(obj["par" + i], 0));
					}
				}
				
				document.objControl.finishParList(res);
			});
		}
		// 등록
		, registSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "registScoreCard";
			if(arg["인자예약코드"]){
				serviceName = "updateMyScoreCard";
				arg["예약코드"] = arg["인자예약코드"];
			}
			var options = {
				"reservSeq": arg["예약코드"]
				, "scoreSeq1": arg["스코어코드0"]
				, "scoreSeq2": arg["스코어코드1"]
				, "scoreSeq3": arg["스코어코드2"]
				, "roundDate": arg["라운드일자"]
				, "golfclubId": arg["골프장코드"]
				, "courseInfoId1": arg["코스코드1"]
				, "courseInfoId2": arg["코스코드2"]
				, "courseInfoId3": arg["코스코드3"]
				, "holeCnt1": arg["홀1"]
				, "holeCnt2": arg["홀2"]
				, "holeCnt3": arg["홀3"]
				, "fileSeq": arg["파일코드"]
				, "saveFileName": arg["파일명"]
				, "orgnFileName": arg["원본파일명"]
				, "contents": arg["내용"]
			};
			$.each(arg["파"], function(n){
				options["par" + (n+1)] = this;
			});
			$.each(arg["스코어"], function(n){
				options["score" + (n+1)] = this;
			});
			
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishRegistSubmit(res);
			});
		}
		// 스코어카드정보
		, getScoreCardInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "getMyScoreCard";
			var options = {
				"reservSeq": arg["스코어카드코드"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var golfCourseName = "";
						if(arg["스코어카드코드"].substrEx(0, 1) == "D"){
							golfCourseName = _util.nvl(obj["golfclubId"]);
						}else{
							golfCourseName = _util.nvl(obj["golfclubName"]) + "/" + _util.nvl(obj["courseInfoName"]);
						}
						
						if(i==0){
							res["결과값"] = {
								"날짜": _util.nvl(obj["reservDate"]).replace(/\./g, "-")
								, "스코어코드": []
								, "요일": _util.nvl(obj["week"])
								, "골프장코스명": golfCourseName
								, "홀": 0
								, "파일정보": {
									"파일코드": _util.nvl(obj["fileSeq"])
									, "파일명": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileName"])
									, "원본파일명": _util.nvl(obj["orgnFileName"])
								}
								, "내용": _util.nvl(obj["contents"])
								, "파": []
								, "타": []
								, "스코어": []
							}
						}
						
						res["결과값"]["홀"] += _util.nvl(obj["holeCnt" + (i+1)], 0);
						res["결과값"]["스코어코드"].push(_util.nvl(obj["scoreSeq"]));
						
						for(j=0;j<_util.nvl(obj["holeCnt" + (i+1)], 0);j++){
							res["결과값"]["파"].push(_util.nvl(obj["par" + (j+1) + "h"], 0));
							res["결과값"]["스코어"].push(_util.nvl(obj["score" + (j+1) + "h"], 0));
							
							var p = res["결과값"]["파"][res["결과값"]["파"].length-1];
							var s = res["결과값"]["스코어"][res["결과값"]["스코어"].length-1];
							
							res["결과값"]["타"].push(s-p);
						}
					}
				}
				
				document.objControl.finishScoreCardInfo(res);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objMyReserveList = null;
		this.objRoundDate = null;
		this.objGolfCourse = null;
		this.objHole = null;
		this.objUploadFile = null;
		this.objNote = null;
		this.objScoreCard = null;
		this.objScoreCardPar = null;
		this.objScoreCardTa = null;
		this.objScoreCardScore = null;
		this.objScoreCardStat = null;
		
		this.scoreCardKey = null;
		this.scoreKeys = null;
		
		this._permitStart = null;
		this.reserveCount = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objMyReserveList = new clsMyReserveList();
			this.objRoundDate = new clsRoundDate();
			this.objGolfCourse = new clsGolfCourse();
			this.objHole = new clsHole();
			this.objUploadFile = new clsUploadFile();
			this.objNote = new clsNote();
			this.objScoreCard = new clsScoreCard();
			this.objScoreCardPar = new clsScoreCardPar();
			this.objScoreCardTa = new clsScoreCardTa();
			this.objScoreCardScore = new clsScoreCardScore();
			this.objScoreCardStat = new clsScoreCardStat();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.scoreCardKey = _util.getParam("q_seq");
			this.scoreKeys = [];
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				"나의예약정보": false
				, "파정보": false
			}
			this.reserveCount = 0;

			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			// 나의 예약정보
			this.objComm.getMyReserveList();
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 예약정보 선택
			this.objMyReserveList.$getLayer().on("change", "select", function(){
				thisClass.setRoundInfo();
			});
			
			// 직접입력
			this.objMyReserveList.$getLayer().on("click", "input:checkbox", function(){
				setTimeout(function(){
					thisClass.setRoundInfo();
					thisClass.setScoreCard();
				}, 100);
			});
			
			// 홀 직접입력
//			this.objHole.$getLayer().on("change", "input:text", function(){
//				var nv = parseInt($(this).val()) || 18;
//				if(nv+"" != $(this).val()) $(this).val(nv);
//				
//				thisClass.setScoreCardTable();
//			});
			
			// 파 직접입력
			this.objScoreCardPar.$getLayer().on("keyup change", "input:text", function(){
				var n = thisClass.objScoreCardPar.$getLayer().find("input:text").index($(this));
				
				var nv = parseInt($(this).val()) || 0;
				if(nv+"" != $(this).val()) $(this).val(nv);
				
				thisClass.setScoreCard();
			})
			
			// 타 직접입력
			this.objScoreCardTa.$getLayer().on("keyup change", "input:text", function(){
				var n = thisClass.objScoreCardTa.$getLayer().find("input:text").index($(this));
				var par = thisClass.objScoreCardPar.getValue(n);
				
				if(par < 3){
					$(this).val(0);
				}else{
					var nv = parseInt($(this).val());
				
					if(isNaN(nv)) nv = 0;
					if(nv+"" != $(this).val()){
						$(this).val(nv);
					}
				}
				
				thisClass.setScoreCard();
			});
			
			// 타 버튼조절
			this.objScoreCardTa.$getLayer().on("click", "span.up, span.down", function(){
				var cName = $(this).attr("class");
				var $obj = thisClass.objScoreCardTa.$getLayer().find("span." + cName);
				var n = $obj.index($(this));
				var par = thisClass.objScoreCardPar.getValue(n);
				
				if(par < 3) return;
				
				
				var aCalcVal = {
					"up": 1
					, "down": -1
				}
				
				thisClass.objScoreCardTa.setValue(n, aCalcVal[cName]);
				thisClass.setScoreCard();
			});
			
			// 등록
			$("div#container").on("click", "img[alt='등록'], img[alt='입력초기화']", function(){
				var bName = $(this).attr("alt");
				
				switch(bName){
					case "등록": 
						if(!thisClass.objMyReserveList.valid()){
							alert(thisClass.objMyReserveList.getError());
							thisClass.objMyReserveList.focus();
							return;
						}else if(!thisClass.objRoundDate.valid()){
							alert(thisClass.objRoundDate.getError());
							thisClass.objRoundDate.focus();
							return;
						}else if(!thisClass.objGolfCourse.valid()){
							alert(thisClass.objGolfCourse.getError());
							thisClass.objGolfCourse.focus();
							return;
						}else if(!thisClass.objHole.valid()){
							alert(thisClass.objHole.getError());
							thisClass.objHole.focus();
							return;
						}
						
						thisClass.submit();
						break;
					case "입력초기화":
						thisClass.reset();
				}
				
				
			});
		}
		, _start: function(){
			$("div#container dl.score_search").hide();
			
			$("div#container img[alt='나의 스코어']").parent("a").attr("id", "idPermissionLink");
			
			if(this.scoreCardKey){
				_gf.spinStart();
				// 스코어 카드 정보
				this.objComm.getScoreCardInfo({
					"스코어카드코드": this.scoreCardKey
				});
			}else{
				this.setScoreCardTable();
				this.setRoundInfo();
				this.setScoreCard();
			}
		}
		// 등록
		, submit: function(){
			if(this.objMyReserveList.getValue() < 0){
				var golfKey = this.objGolfCourse.getValue();
				var courseKey1 = "";
				var courseKey2 = "";
				var hole1 = this.objHole.getValue();
				var hole2 = "";
				var hole3 = "";
			}else{
				var golfKey = this.objMyReserveList.getGolfKey();
				var courseKey1 = this.objMyReserveList.getCourseKey1();
				var courseKey2 = this.objMyReserveList.getCourseKey2();
				var hole1 = this.objReserveList.getHole(1);
				var hole2 = this.objReserveList.getHole(2);
				var hole3 = this.objReserveList.getHole(3);
			}
			
			var op = {
				"인자예약코드": this.scoreCardKey
				, "예약코드": this.objMyReserveList.getReserveKey()
				, "라운드일자": this.objRoundDate.getValue().replace(/-/g, "")
				, "골프장코드": golfKey
				, "코스코드1": courseKey1
				, "코스코드2": courseKey2
				, "코스코드3": ""
				, "홀1": hole1
				, "홀2": hole2
				, "홀3": hole3
				, "파": this.objScoreCardPar.getValues()
				, "스코어": this.objScoreCardScore.getValues()
				, "파일코드": this.objUploadFile.getFileKey()
				, "파일명": this.objUploadFile.getFilePath()
				, "원본파일명": this.objUploadFile.getOrigFileName()
				, "내용": this.objNote.getValue()
			}
			
			for(var i=0;i<3;i++){
				op["스코어코드" + i] = _util.nvl(this.scoreKeys[i]);
			}
			
			this.objComm.registSubmit(op);
		}
		// 초기화
		, reset: function(){
			
		}
		// 라운드일자, 골프장코스, 홀 출력, 파설정
		, setRoundInfo: function(){
			var n = this.objMyReserveList.getValue();
				
			if(n < 0){
				this.objRoundDate.setEditable(true);
				this.objGolfCourse.setEditable(true);
				this.objHole.setEditable(true);
				
				this.objRoundDate.setValue(this.objMyReserveList.getRoundDate());
				this.objGolfCourse.setValue(this.objMyReserveList.getGolfCourse());
				this.objHole.setValue(18);
				//this.objHole.$getLayer().find("input:text").attr("DISABLED", "DISABLED");
				
				this.objScoreCardPar.editable();
			}else{
				this.objRoundDate.setEditable(false);
				this.objGolfCourse.setEditable(false);
				this.objHole.setEditable(false);
				
				this.objRoundDate.setValue(this.objMyReserveList.getRoundDate());
				this.objGolfCourse.setValue(this.objMyReserveList.getGolfCourse());
				this.objHole.setValue("");
				
				this.objScoreCardPar.setList(this.objMyReserveList.getParValues());
			}
		}
		// 스코어카드 세팅
		, setScoreCardTable: function(){
//			var n = this.objMyReserveList.getValue();
//			//this.objHole.$getLayer().find("input:text").trigger("change");
//			
//			var h;
//			if(n < 0){
//				h = this.objHole.getValue();
//			}else{
//				h = this.objMyReserveList.getHole();
//			}
//			
//			h = h || 18;
//			
//			this.objScoreCard.setTable(h);
		}
		// 홀별 기록 설정
		, setScoreCard: function(){
			this.objScoreCardPar.calculate();
			this.objScoreCardTa.calculate();
			
			this.objScoreCardScore.setList({
				"파": this.objScoreCardPar.getValues()
				, "타": this.objScoreCardTa.getValues()
			});
			
			this.objScoreCardStat.setList({
				"파": this.objScoreCardPar.getValues()
				, "타": this.objScoreCardTa.getValues()
			});
		}
		// 나의 예약정보 완료
		, finishMyReserveList: function(ret){
			var thisClass = this;
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisData = this.objComm.getMyReserveData();
			
			if(this.reserveCount == 0){
				this._permitStart["파정보"] = true;
			}
			
			this.reserveCount = thisData.length;
			
			this.objMyReserveList.setList(thisData);
			
			$.each(thisData, function(){
				thisClass.objComm.getParList({
					"예약코드": this["예약코드"]
					, "골프장코드": this["골프장코드"]
					, "코스코드1": this["코스코드1"]
					, "코스코드2": this["코스코드2"]
				});
			});
			
			this._permitStart["나의예약정보"] = true;
		}
		// 파일첨부 완료
		, finishUploadFile: function(ret){
			this.objUploadFile.setInfo(ret["파일리스트"][0]);
		}
		// 등록완료
		, finishRegistSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			document.location.href = _ROOT + "/mypage/my_score.html";
		}
		// 코스스코어 상세 완료
		, finishParList: function(ret){
			if(ret["결과코드"] != "00000") return;
			
			this.objMyReserveList.setPar(ret["예약코드"], ret["파"]);
			
			this.reserveCount--;
			if(this.reserveCount == 0){
				this._permitStart["파정보"] = true;
			}
		}
		// 스코어카드정보 완료
		, finishScoreCardInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			this.objMyReserveList.setReserveKey(this.scoreCardKey);
			this.setScoreCardTable();
			this.setRoundInfo();
			
			if(this.scoreCardKey.substrEx(0, 1) == "D"){
				this.objRoundDate.setValue(ret["결과값"]["날짜"]);
				this.objGolfCourse.setValue(ret["결과값"]["골프장코스명"]);
				this.objHole.setValue(ret["결과값"]["홀"]);
				
				this.objScoreCardPar.setValues(ret["결과값"]["파"]);
				this.objScoreCardTa.setValues(ret["결과값"]["타"]);
			}else{
				
			}
			
			this.objUploadFile.setInfo(ret["결과값"]["파일정보"]);
			this.objNote.setValue(ret["결과값"]["내용"]);
			
			this.scoreKeys = ret["결과값"]["스코어코드"];
			
			this.setScoreCard();
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
